<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class GroupAssistantLog extends Model
{
    protected $table = "c_group_assistant_logs";

    public function group_assistant()
    {
        return $this->hasOne('App\Models\GroupAssistant', 'id', 'group_assistant_id');
    }

    public function book()
    {
        return $this->hasOne('App\Models\AddressBook', 'id', 'book_id');
    }



    /***
     * @param $filter
     * @return mixed
     * 获取群发记录
     */
    public function getList($filter)
    {
        $res = $this->where([['work_id', '=', $filter['work_id']],['type','=',$filter['type']]])->with(['group_assistant','book'])->orderBy("id","desc")->simplePaginate(15)->toArray();
        $host = new Url();
        if(empty($res['data'])){
            return $res;
        }
        foreach($res['data'] as $k=>&$v){
            $v['group_assistant']['sound'] = !empty($v['group_assistant']['sound']) ? $host::getHttpHost() . 'upload/' . $v['group_assistant']['sound'] : "";
            $v['group_assistant']['video'] = !empty($v['group_assistant']['video']) ? $host::getHttpHost() . 'upload/' . $v['group_assistant']['video'] : "";
            if (!empty($v['group_assistant']['images'])) {
                foreach ($v['group_assistant']['images'] as $kk => &$vv) {
                    $vv = $host::getHttpHost() . 'upload/' . $vv;
                }
            }
            //获取标签
            $v['book']["label_name"] = (new AddressBookRelateLabel())
                ->join("c_address_book_labels as label","label.id",'=','c_address_book_relate_labels.address_book_label_id')
                ->select("label.label_name")
                ->where([["c_address_book_relate_labels.address_book_id","=",$v['book_id']]])->get();
        }
        return $res;
    }


}
